package com.example.s.Activity.login;

import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.s.Activity.BaseActivity;
import com.example.s.R;
import com.example.s.adapter.MyLoopViewAdapter;
import com.example.s.utils.MyUtils;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends BaseActivity {
    private ViewPager vp;
    private LinearLayout indicatorContainer;private TextView hint;

    private void initView() {

        hint = (TextView) findViewById(R.id.hint);

        vp = (ViewPager) findViewById(R.id.vp);
        indicatorContainer = (LinearLayout) findViewById(R.id.indicatorContainer);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initView();

        List<Integer> mData = new ArrayList<>();
        mData.add(R.mipmap.guide_page_bg);
        mData.add(R.mipmap.guide_page_bg1);
        mData.add(R.mipmap.guide_page_bg2);
        vp.setAdapter(new MyLoopViewAdapter(mData));
        for (int i = 0; i < mData.size(); i++) {
            ImageView imageView = new ImageView(this);
            imageView.setBackgroundResource(R.drawable.indicator_bg);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.leftMargin = MyUtils.getInstance().dip2px(this, 6);
            layoutParams.rightMargin = MyUtils.getInstance().dip2px(this, 6);

            if (i == 0) {
                layoutParams.leftMargin = MyUtils.getInstance().dip2px(this, 0);
                imageView.setBackgroundResource(R.drawable.indicator_bg_select);

            }else if(i==mData.size()-1){
                layoutParams.rightMargin = MyUtils.getInstance().dip2px(this, 0);
            }
            indicatorContainer.addView(imageView,layoutParams);

        }
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position==indicatorContainer.getChildCount()-1){
                    hint.setVisibility(View.VISIBLE);
                    indicatorContainer.setVisibility(View.INVISIBLE);
                }else {
                    hint.setVisibility(View.INVISIBLE);
                    indicatorContainer.setVisibility(View.VISIBLE);
                }
                for (int i = 0; i < indicatorContainer.getChildCount(); i++) {
                    indicatorContainer.getChildAt(i).setBackgroundResource(R.drawable.indicator_bg);
                    if(i==position){
                    indicatorContainer.getChildAt(i).setBackgroundResource(R.drawable.indicator_bg_select);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        hint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(GuideActivity.this,LetterActivity.class));
                finish();

            }
        });
    }


}
